package com.kaibes.module.nacos;

import java.util.List;

import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Value;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;

public abstract class NacosLastListener {

    @Value("${spring.application.name:}")
    private String service;
    @NacosInjected
    private NamingService naming;
    
    public abstract String getName();
    public abstract String getGroup();
    public abstract void preDestory(String service);
    
    @PreDestroy
    public void exit() {
        try {
            List<Instance> instances = naming.getAllInstances(getName(), getGroup());
            if (instances.size() == 1) {
                preDestory(service);
            }
        } catch (NacosException e) {
            e.printStackTrace();
        }
    }

}
